import { commonRouters, SystemRouter, CmsRouter } from "@/router";
import { setSessionStorage } from "../../utils/storage";

const permission = {
    state: {
        routers: null,
        addRouters: [],
    },
    mutations: {
        SET_ROUTERS: (state, routers) => {
            state.addRouters = routers
            state.routers = commonRouters.concat(routers)
        }
    },
    actions: {
        GeneratorRouters({ commit }, data) { //生成路由表
            return new Promise((resolve, reject) => {
                commit('SET_ROUTERS', data)
                resolve()
            })
        },
        ChangeRouters({ dispatch, commit, state }, routerType) { //切换路由表
            return new Promise((resolve, reject) => {
                if('CMS' === routerType) {
                    commit('SET_ROUTERS', CmsRouter)
                } else {
                    commit('SET_ROUTERS', SystemRouter)
                }
                setSessionStorage('RouterType', routerType)

                //刷新页面，达到清空原有路由表
                window.location = ''

                resolve()
            })
        }
    }
}

export default permission

